package com.ruoyi.activity.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.shop.api.RemoteShopService;
import com.ruoyi.shop.api.domain.CheShop;
import io.swagger.v3.core.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.activity.mapper.CheActivityMapper;
import com.ruoyi.activity.api.domain.CheActivity;
import com.ruoyi.activity.service.ICheActivityService;

import static com.ruoyi.common.core.web.domain.AjaxResult.DATA_TAG;

/**
 * 活动项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@Service
public class CheActivityServiceImpl implements ICheActivityService 
{
    @Autowired
    private CheActivityMapper cheActivityMapper;
    @Autowired
    private RemoteShopService remoteShopService;

    /**
     * 查询活动项目
     * 
     * @param id 活动项目主键
     * @return 活动项目
     */
    @Override
    public CheActivity selectCheActivityById(Long id)
    {
        CheActivity cheActivity = cheActivityMapper.selectCheActivityById(id);
        Long shopId = cheActivity.getShopId();
        AjaxResult result = remoteShopService.getInfo(shopId, null);
        Object o =result.get(AjaxResult.DATA_TAG);
        if (o == null) {
            return cheActivity;
        }
        CheShop cheShop = JSON.parseObject(JSON.toJSON(o).toString(), CheShop.class);
        cheActivity.setCheShop(cheShop);
        return cheActivity;
    }

    /**
     * 查询活动项目列表
     * 
     * @param cheActivity 活动项目
     * @return 活动项目
     */
    @Override
    public List<CheActivity> selectCheActivityList(CheActivity cheActivity)
    {
        List<CheActivity> cheActivities = cheActivityMapper.selectCheActivityList(cheActivity);
        cheActivities.forEach(item->{
            Long shopId = item.getShopId();
            AjaxResult result = remoteShopService.getInfo(shopId, null);
            Object o =result.get(AjaxResult.DATA_TAG);
            if (o == null) {
                return;
            }
            CheShop cheShop = JSON.parseObject(JSON.toJSON(o).toString(), CheShop.class);
            item.setCheShop(cheShop);
        });
        return cheActivities;
    }

    /**
     * 新增活动项目
     * 
     * @param cheActivity 活动项目
     * @return 结果
     */
    @Override
    public int insertCheActivity(CheActivity cheActivity)
    {
        cheActivity.setCreateTime(DateUtils.getNowDate());
        return cheActivityMapper.insertCheActivity(cheActivity);
    }

    /**
     * 修改活动项目
     * 
     * @param cheActivity 活动项目
     * @return 结果
     */
    @Override
    public int updateCheActivity(CheActivity cheActivity)
    {
        return cheActivityMapper.updateCheActivity(cheActivity);
    }

    /**
     * 批量删除活动项目
     * 
     * @param ids 需要删除的活动项目主键
     * @return 结果
     */
    @Override
    public int deleteCheActivityByIds(Long[] ids)
    {
        return cheActivityMapper.deleteCheActivityByIds(ids);
    }

    /**
     * 删除活动项目信息
     * 
     * @param id 活动项目主键
     * @return 结果
     */
    @Override
    public int deleteCheActivityById(Long id)
    {
        return cheActivityMapper.deleteCheActivityById(id);
    }
}
